import java.util.ArrayList;

/**
 * @author Jesse McCane
 * @author Berkley Shands
 */

/*
 * This class deals with human player specific functionality
 */

public class Human extends Player{

	Human(){
		this.myName = "Player " + playerCount;
		playerCount++;
		totalCount++;
		this.myIDNum = totalCount;
	}

	Human(String name){
		this.myName = name;
		totalCount++;
		this.myIDNum = totalCount;
	}

	public void setPlayableCards(Card[] cardsPlayed, int firstCard, int numPlayed){
		playableCards = new ArrayList<Boolean>(myHand.size());
		if((Gameplay.roundTrump == Gameplay.ALLTRUMP) || (numPlayed == 0)){
			for(int i = 0; i < myHand.size(); i++){
				playableCards.add(i, true);
			}
			return;
		}
		for(int i = 0; i < myHand.size(); i++){
			if(myHand.get(i).isAOH()){
				playableCards.add(i, true);
			}else if(myHand.get(i).getSuit() == Gameplay.roundTrump){
				playableCards.add(i, true);
			}else if(cardsPlayed[firstCard].isAOH()){
				if(!(this.hasSuit(Gameplay.roundTrump))){
					playableCards.add(i, true);
				}else{
					playableCards.add(i, false);
				}
			}else{
				if(myHand.get(i).getSuit() == cardsPlayed[firstCard].getSuit()){
					playableCards.add(i, true);
				}else if(!(this.hasSuit(cardsPlayed[firstCard].getSuit()))){
					playableCards.add(i, true);
				}else{
					playableCards.add(i, false);
				}
			}
		}
	}

	public void displayPlayableHand(){
		System.out.println(myName + "'s playable cards in this situation are:");
		for(int i = 0; i < myHand.size(); i++){
			if(playableCards.get(i)){
				System.out.println("" + (i + 1) + " - " + myHand.get(i).toString());
			}
		}
		System.out.println("");
	}

	public void firstBid(){
		String promptStr;
		int tempCall, tempTrump, tempBid;

		this.displayHand();

		System.out.println("1 - Bid \n2 - Nil");

		promptStr = myName + ", please make a selection: ";
		tempCall = Gameplay.getNumberInput(promptStr, 1, 2);

		if(tempCall == Gameplay.BID){
			tempBid = Gameplay.getNumberInput("Please enter your bid: ", 1, Gameplay.handSize);
		}else{//niled
			tempBid = 0;
		}
		Gameplay.printTrumpOptions();
		tempTrump = Gameplay.getNumberInput("Please enter your trump: ", 1, Gameplay.NUM_TRUMP_OPTIONS);

		this.setBidParams(tempCall, tempBid, tempTrump);
	}

	public int bid(int maxTrump, int maxBid, int bCounter){
		String promptStr;
		int tempCall;
		boolean outbid = false;

		this.displayHand();

		promptStr = myName + ", please make a selection: ";
		Gameplay.printCallOptions();
		tempCall = Gameplay.getNumberInput(promptStr, 1, Gameplay.NUM_CALL_OPTIONS);

		if(tempCall == Gameplay.BID){
			outbid = doBid(maxTrump, maxBid);
		}else if(tempCall == Gameplay.NIL){
			outbid = doNil(maxTrump, maxBid);
		}else if(tempCall == Gameplay.PASS){
			doPass();
		}else if(tempCall == Gameplay.CALL){
			doCall(maxTrump, maxBid);
		}

		if(outbid){
			bCounter = 1;
		}else{
			bCounter++;
		}

		return bCounter;
	}

	protected boolean doNil(int maxTrump, int maxBid){
		int tempTrump;
		boolean outbid = false;

		if(maxBid == 0){ // lead bid was a nil
			if(maxTrump != Gameplay.NOTRUMP){//anything but notrump we need to prompt suit
				Gameplay.printTrumpOptions();
				tempTrump = Gameplay.getNumberInput("Please enter your trump: ", maxTrump, Gameplay.NUM_TRUMP_OPTIONS);
				if(tempTrump != maxTrump){ //raised nil trump suit
					maxTrump = tempTrump;
					outbid = true;
				}
			}
		}

		this.setBidParams(Gameplay.NIL, 0, maxTrump);

		return outbid;
	}

	protected boolean doBid(int maxTrump, int maxBid){
		int tempBid, tempTrump, minSuit, minBid;
		boolean outbid = false;

		if(maxBid == 0){
			System.out.println("The current bid is nil " + Gameplay.trumpNames.get(maxTrump) + ".\n");
			minBid = 1;
		}else{
			System.out.println("The current bid is " + maxBid + " " + Gameplay.trumpNames.get(maxTrump) + ".\n");
			minBid = maxBid;
		}

		tempBid = Gameplay.getNumberInput("Please enter your bid: ", minBid, Gameplay.handSize);
		if(tempBid > maxBid){
			minSuit = 1;
		}else{
			minSuit = maxTrump;
		}

		Gameplay.printTrumpOptions();
		tempTrump = Gameplay.getNumberInput("Please enter your trump: ", minSuit, Gameplay.NUM_TRUMP_OPTIONS);

		//checks for a raised bid
		if((tempBid != maxBid) || (tempTrump != maxTrump)){
			maxTrump = tempTrump;
			maxBid = tempBid;
			outbid = true;
		}

		this.setBidParams(Gameplay.BID, maxBid, maxTrump);

		return outbid;
	}

	public Card playTrick(){
		int cardNum;
		Card playedCard;

		this.displayHand();
		this.displayPlayableHand();

		cardNum = Gameplay.getNumberInput("Please pick a card to play ", 1, this.myHand.size());
		while(!(this.playableCards.get(cardNum - 1))){
			System.out.println("That is not a valid card to play.");
			cardNum = Gameplay.getNumberInput("Please pick a card to play ", 1, this.myHand.size());
		}

		playedCard = this.playCard(cardNum - 1); //-1 because cards are 0-X not 1-X+1

		return playedCard;
	}

	//this does nothing for humans
	public void determineStrat(){}

	public void adjustRoundStrat(){}
}
